﻿namespace Vdc.Grid
{
    using System;
    using System.Collections;
    using System.Reflection;
    using System.Web.UI;

    public class ColumnCollection : GridPersistentCollection, INamingContainer
    {
        internal Vdc.Grid.Grid Grid;

        public void Add(Column oCol)
        {
            if (oCol.Index == -1)
            {
                oCol.Index = this.GetMaxIndex() + 1;
            }
            int columnIndexByDataField = this.GetColumnIndexByDataField(oCol.DataField);
            if ((columnIndexByDataField == -1) || string.IsNullOrEmpty(oCol.DataField))
            {
                oCol.GridContainer = this.Grid;
                base.List.Add(oCol);
            }
            else
            {
                ((Column) base.List[columnIndexByDataField]).CopyRelevantPropertiesFromColumn(oCol);
            }
        }

        public Column GetColumnByDataField(string sDataField)
        {
            foreach (Column column in base.List)
            {
                if (column.DataField.ToLower() == sDataField.ToLower())
                {
                    return column;
                }
            }
            return null;
        }

        public Column GetColumnByIndex(int index)
        {
            foreach (Column column in base.List)
            {
                if (column.Index == index)
                {
                    return column;
                }
            }
            return null;
        }

        public Column GetColumnByTempDataField(string sDataField)
        {
            foreach (Column column in base.List)
            {
                if (column.TempDataField.ToLower() == sDataField.ToLower())
                {
                    return column;
                }
            }
            return null;
        }

        public int GetColumnIndexByDataField(string sDataField)
        {
            int num = 0;
            foreach (Column column in base.List)
            {
                if (column.DataField.ToLower() == sDataField.ToLower())
                {
                    return num;
                }
                num++;
            }
            return -1;
        }

        private int GetMaxIndex()
        {
            int index = -1;
            foreach (Column column in base.List)
            {
                if (column.Index > index)
                {
                    index = column.Index;
                }
            }
            return index;
        }

        public void Insert(int position, Column oCol)
        {
            for (int i = position; i < base.List.Count; i++)
            {
                Column column1 = (Column) base.List[i];
                column1.Index++;
            }
            base.List.Insert(position, oCol);
        }

        internal bool NeedIndexRegeneration()
        {
            foreach (Column column in base.List)
            {
                if (column.Index < 0)
                {
                    return true;
                }
            }
            return false;
        }

        public void Remove(Column _col)
        {
            this.UpdateColumnIndexes(_col);
            base.List.Remove(_col);
        }

        public void Remove(int i)
        {
            this.UpdateColumnIndexes((Column) base.List[i]);
            base.List.RemoveAt(i);
        }

        public void Sort()
        {
            IComparer comparer = new SortHelper();
            base.InnerList.Sort(comparer);
        }

        public void SortByGroupByPriority()
        {
            IComparer comparer = new SortByGroupByPriorityHelper();
            base.InnerList.Sort(comparer);
        }

        public void SortBySortPriority()
        {
            IComparer comparer = new SortBySortPriorityHelper();
            base.InnerList.Sort(comparer);
        }

        private void UpdateColumnIndexes(Column _col)
        {
            foreach (Column column in base.List)
            {
                if (column.Index > _col.Index)
                {
                    column.Index--;
                }
            }
        }

        public Column this[string dataField]
        {
            get
            {
                try
                {
                    return this.GetColumnByDataField(dataField);
                }
                catch (Exception exception1)
                {
                    if (exception1 != null)
                    {
                    }
                    return null;
                }
            }
        }

        public Column this[int index]
        {
            get
            {
                try
                {
                    return (Column) base.List[index];
                }
                catch (Exception exception1)
                {
                    if (exception1 != null)
                    {
                    }
                    return null;
                }
            }
        }

        private class SortByGroupByPriorityHelper : IComparer
        {
            public int Compare(object x, object y)
            {
                Column column = (Column) x;
                Column column2 = (Column) y;
                if (column.GroupByPriority > column2.GroupByPriority)
                {
                    return 1;
                }
                if (column.GroupByPriority < column2.GroupByPriority)
                {
                    return -1;
                }
                return 0;
            }
        }

        private class SortBySortPriorityHelper : IComparer
        {
            public int Compare(object x, object y)
            {
                Column column = (Column) x;
                Column column2 = (Column) y;
                if (column.GroupBy == column2.GroupBy)
                {
                    if (column.GroupByPriority == column2.GroupByPriority)
                    {
                        if (column.SortPriority > column2.SortPriority)
                        {
                            return 1;
                        }
                        if (column.SortPriority < column2.SortPriority)
                        {
                            return -1;
                        }
                        return 0;
                    }
                    if (column.GroupByPriority > column2.GroupByPriority)
                    {
                        return 1;
                    }
                    if (column.GroupByPriority < column2.GroupByPriority)
                    {
                        return -1;
                    }
                    return 0;
                }
                if (column.GroupBy)
                {
                    return -1;
                }
                return 1;
            }
        }

        private class SortHelper : IComparer
        {
            public int Compare(object x, object y)
            {
                Column column = (Column) x;
                Column column2 = (Column) y;
                if (column.Index > column2.Index)
                {
                    return 1;
                }
                if (column.Index < column2.Index)
                {
                    return -1;
                }
                return 0;
            }
        }
    }
}

